<!--添加联系人对话框-->
<template>
    <div>
        <el-dialog v-model="dialogVisible" align-center width="600" title="添加联系人" :before-close="handleClosed">
            <el-form v-model="queryData">
                <el-row>
                    <el-form-item label="称呼">
                        <el-input v-model="queryData.name"/>
                    </el-form-item>
                    <el-form-item label="备注" label-width="100">
                        <el-input v-model="queryData.comments"/>
                    </el-form-item>
                </el-row>
                <el-row>
                    <el-form-item label="手机">
                        <el-input v-model="queryData.phone"/>
                    </el-form-item>
                    <el-form-item label="邮箱" label-width="100">
                        <el-input v-model="queryData.email"/>
                    </el-form-item>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button v-if="isEdit" type="primary" @click="handleEdit">编辑</el-button>
                    <el-button v-else type="primary" @click="handleAdd">保存</el-button>
                    <el-button @click="handleClosed">取 消</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import contactAPI from "@/util/request-md/contact/contact.js";

export default {
    emits: ['callClosed', 'callSaveSuccess'],
    props: ['idSupport', 'visible', 'enableCustomer', 'isEdit', 'info'],
    name: "AddContactDialog",
    data() {
        return {
            dialogVisible: false,
            queryData: {
                name: '',
                phone: '',
                email: '',
                comments: '',
                idContact: '',
                id:'',
            }
        }
    },
    watch: {
        'visible'(val) {
            if (this.isEdit) {
                this.queryData = {
                    name: this.info.name,
                    phone: this.info.phone,
                    email: this.info.email,
                    comments: this.info.comments,
                    idContact:this.info.idContact
                }
                console.log(this.info,">>>>>>加载的时候传过来的值")
            }
            this.dialogVisible = val;
        },
        'idSupport'(val) {
            this.queryData.id = val;
        },
    },
    methods: {
        handleClosed() {
            this.$emit('callClosed');
        },
        handleSaveSuccess() {
            this.$emit('callSaveSuccess');
        },
        handleEdit() {
            this.queryData.id = this.idSupport;
            if(this.enableCustomer){
                console.log(this.queryData,">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
                contactAPI.updateCustContact(this.queryData, (res) => {
                    if (res.code === 200) {
                        this.$message({
                            type: 'success',
                            message: '修改成功!'
                        });
                        this.queryData = {
                            name: '',
                            phone: '',
                            email: '',
                            comments: '',
                            id: '',
                            idContact: ''
                        }
                        this.handleSaveSuccess();
                    } else {
                        this.$message.error("保存异常:" + res.code);
                    }
                })
            }else{
                console.log(this.queryData,">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
                contactAPI.updateContact(this.queryData, (res) => {
                    if (res.code === 200) {
                        this.$message({
                            type: 'success',
                            message: '保存成功!'
                        });
                        this.queryData = {
                            name: '',
                            phone: '',
                            email: '',
                            comments: '',
                            id: '',
                            idContact: ''
                        }
                        this.handleSaveSuccess();
                    } else {
                        this.$message.error("保存异常:" + res.code);
                    }
                })
            }
        },
        handleAdd() {
            if (this.enableCustomer) {
                contactAPI.addCustContact(this.queryData, (res) => {
                    if (res.code === 200) {
                        this.$message({
                            type: 'success',
                            message: '保存成功!'
                        });
                        this.queryData = {
                            name: '',
                            phone: '',
                            email: '',
                            comments: '',
                            id: '',
                            idContact: ''
                        }
                        this.handleSaveSuccess();
                    } else {
                        this.$message.error("保存异常:" + res.code);
                    }
                })
            } else {
                contactAPI.addContact(this.queryData, (res) => {
                    if (res.code === 200) {
                        this.$message({
                            type: 'success',
                            message: '保存成功!'
                        });
                        this.queryData = {
                            name: '',
                            phone: '',
                            email: '',
                            comments: '',
                            id: '',
                            idContact: ''
                        }
                        this.handleSaveSuccess();
                    } else {
                        this.$message.error("保存异常:" + res.code);
                    }
                })
            }

        }
    }
}
</script>

<style scoped>

</style>